<template>
  <div class="center">
    <div class="title">
      <TextArea :rows="1" :style="{ width: '260px', 'min-height': '60px', margin: '16px auto', 'font-size': '24px', 'line-height': '28px', 'text-align': 'center' }" :modelValue="newSurveyData.title" @update:modelValue="newSurveyData.modifyTitle($event)" :placeholder="'请输入标题'" />
    </div>
    <div class="info">
      <TextArea :rows="4" :modelValue="newSurveyData.info" @update:modelValue="newSurveyData.modifyInfo($event)" placeholder="问卷说明 / 介绍（选填）" />
    </div>

    <!-- 这里的 for 中的 key，不能使用 order, 因为 order 会变化（当上移、下移、往上方添加元素） -->
    <div class="center-item" v-for="(q, i) in newSurveyData.questions" :key="q.id" :class="{ 'q-active': newSurveyData.currentOrder === i }" @click.stop="newSurveyData.modifyCurrentOrder(q.order)">
      <template v-if="q.type === QUESTION_TYPE_DESCRIPTION">
        <SpanText :text="q.order + 1 + COMPONENT_TYPE_INFO[q.type]"></SpanText>
        <TextArea :textAreaStyle="{'font-weight':'bold','color': '#2378ff'}" :modelValue="newSurveyData.questions[q.order].title" @update:modelValue="newSurveyData.modifyQuestionTitle(q.order, $event)" :placeholder="'请输入段落描述'" />
      </template>
      <template v-else>
        <span v-show="q.required" class="require-span">*</span>
        <SpanText :text="q.order + 1 + COMPONENT_TYPE_INFO[q.type]"></SpanText>
        <TextArea :modelValue="newSurveyData.questions[q.order].title" @update:modelValue="newSurveyData.modifyQuestionTitle(q.order, $event)" :placeholder="'请输入题目标题'" />
        <TextArea :modelValue="newSurveyData.questions[q.order].info" @update:modelValue="newSurveyData.modifyQuestionInfo(q.order, $event)" :rows="2" :placeholder="'题目说明 / 描述（选填）'" />

        <template v-if="q.type === QUESTION_TYPE_TEXT || q.type ===  QUESTION_TYPE_TEXTAREA">
          <div :style="{height: q.type === QUESTION_TYPE_TEXT? '40px':'90px'}"  class="placeholder-input">供用户输入</div>
        </template>
        <component v-else :is="COMPONENT_NAME[q.type]" :order="q.order"></component>
      </template>
    </div>
  </div>
</template>

<script>
import newSurveyData from '@/store/new-survey-data'
import { COMPONENT_NAME, QUESTION_TYPE_DESCRIPTION, QUESTION_TYPE_TEXT, QUESTION_TYPE_TEXTAREA, COMPONENT_TYPE_INFO } from '@/constants/default'

import TextArea from '@/components/TextArea.vue'
import SpanText from '@/components/SpanText.vue'
import Radio from './_Radio.vue'
import MatrixRadio from './_MatrixRadio.vue'

export default {
  data() {
    return {
      newSurveyData: newSurveyData(),
      COMPONENT_NAME,
      QUESTION_TYPE_DESCRIPTION,
      QUESTION_TYPE_TEXT,
      QUESTION_TYPE_TEXTAREA,
      COMPONENT_TYPE_INFO,
    }
  },
  components: {
    TextArea,
    SpanText,
    Radio,
    MatrixRadio,
  },
  methods: {
  }
}
</script>

<style lang="scss" scoped>

.placeholder-input {
  width: 90%;
  margin: 10px auto;

  border-radius: 2px;
  border: 1px dashed #d9d9d9;
  color: #d9d9d9;
  display: grid;
  place-content: center;

  cursor: default;
}

.center {
  flex: 0 0 auto;
  width: 50vw;
  min-width: 400px;
  max-width: 800px;
  margin-bottom: 100px;

  position: relative;
  z-index: 5;

  display: flex;
  flex-direction: column;
  align-items: center;

  background-color: #fff;

  > .info {
    padding-bottom: 16px;
  }
  > .center-item {
    background-color: #fff;
    transition: transform 0.16s;
    border-radius: 2px;
    padding: 20px 30px;
    position: relative;
    &:hover {
      @include suspend;
    }
    & > .require-span {
      position: absolute;
      color: #f00;
      font-size: 30px;
      top: 40px;
      left: 30px;
    }
  }
  > .center-item.q-active {
  @include suspend;
  & > .span-text {
    color: #fff;
    background-color: #60a5fa;
  }
}
}
</style>
